﻿Public Class FrmDisegniClienti
    Dim files As System.Collections.ObjectModel.ReadOnlyCollection(Of String)
    Dim selezione As String
    Dim estensione As String
    Dim file As String
    Dim ricerca As String
    Dim arfiles As New ArrayList
    Dim stampa_watermark As Integer = 0
    Dim temp1 As System.IO.FileInfo

    Private Sub FrmDisegniClienti_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: questa riga di codice carica i dati nella tabella 'DbporteclientiDataSet1.MVA'. È possibile spostarla o rimuoverla se necessario.
        Me.MVATableAdapter.Fill(Me.DbporteclientiDataSet1.MVA)
        'TODO: questa riga di codice carica i dati nella tabella 'DbporteclientiDataSet1.PORTE_SPECIALI'. È possibile spostarla o rimuoverla se necessario.
        Me.PORTE_SPECIALITableAdapter.Fill(Me.DbporteclientiDataSet1.PORTE_SPECIALI)

        ComboBox1.SelectedIndex = 0
        If rcommesse.Count > 0 Then
            For Each cartella In rcommesse
                If cartella.ToString.EndsWith("TC200") Or cartella.ToString.EndsWith("tc200") Or cartella.ToString.EndsWith("TC5000") Or cartella.ToString.EndsWith("tc5000") Then
                Else
                    ListBox1.Items.Add(cartella)
                End If
            Next
        Else
            For Each cartella In commesse
                If cartella.ToString.EndsWith("TC200") Or cartella.ToString.EndsWith("tc200") Or cartella.ToString.EndsWith("TC5000") Or cartella.ToString.EndsWith("tc5000") Then
                Else
                    ListBox1.Items.Add(cartella)
                End If
            Next
        End If

        For Each ITEM In My.Computer.FileSystem.GetDirectories("\\Lafer2k3\uff_tec\PDF_PROE\")
            ListBox1.Items.Add(ITEM)
        Next

        AxVecad1.Hide()
        Me.Text = "Ricerca disegni in cartelle clienti"

        '------CARICO I RIALZI NELLA LISTA DEI RIALZI------
        CBRIALZO_PIASTRA.Items.Clear()
        For Each ITEM In Clsdatabase.CARICA_DATI(DbporteclientiDataSet1.PORTE_SPECIALI, DbporteclientiDataSet1.PORTE_SPECIALI.Columns("RIALZO/PIASTRA"))
            CBRIALZO_PIASTRA.Items.Add(ITEM)
        Next
        '---------------------------------------------------
        Panel3.Parent = Panel2
        Panel3.Enabled = False
        '------chiudo il pannello a scomparsa---------------
        Button4.Text = "◄"
        Panel2.Enabled = False

        Dim SIZE As New Size
        SIZE.Width = 143
        SIZE.Height = 382

        Dim PUNTO As New Point
        PUNTO.X = Me.Size.Width - SIZE.Width - 25
        PUNTO.Y = Me.Size.Height - SIZE.Height - 39

        Panel2.Parent = Me
        Panel2.SetBounds(PUNTO.X, PUNTO.Y, SIZE.Width, SIZE.Height)
        Label24.Text = ""
        'Button4.Text = "►"
        '---------------------------------------------------

        For Each ITEM In Clsdatabase.Lista_item("COMP_VARI", "TIPOLOGIA")
            Cb_tipologia.Items.Add(ITEM)
        Next

        For Each ITEM In Clsdatabase.Lista_item("PORTE_SPECIALI", "INTERRUTTORE")
            TBINTERRUTTORE.Items.Add(ITEM)
        Next

        Me.Focus()

    End Sub

    Private Sub Btclose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btclose.Click
        Me.Close()
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        selezione = ListBox1.SelectedItem.ToString
        estensione = ComboBox1.SelectedItem.ToString
        ListBox2.Items.Clear()
        arfiles.Clear()

        If BackgroundWorker1.IsBusy = False Then
            BackgroundWorker1.RunWorkerAsync()
        End If


        While BackgroundWorker1.IsBusy
            Application.DoEvents()
            Continue While
        End While

        For Each Me.file In files
            'If file.EndsWith(".pdf") Or file.EndsWith(".PDF") Then
            arfiles.Add(file)
            ' Dim SOTTODIRECTORY As String
            '  SOTTODIRECTORY = My.Computer.FileSystem.GetParentPath(file)
            ' Dim SOTTODIRECTORYES = SOTTODIRECTORY.Split("\")
            ListBox2.Items.Add(My.Computer.FileSystem.GetName(file))

            'End If
        Next

        If ListBox2.Items.Count <= 0 Then
            ListBox2.Items.Add("Non ci sono disegni in questa cartella")
        End If

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        AxAcroPDF1.printAllFit(True)

        MsgBox("Documento inviato alla stampante")
    End Sub

    Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork

        files = My.Computer.FileSystem.GetFiles(selezione, FileIO.SearchOption.SearchTopLevelOnly, "*" & estensione)
    End Sub


    Private Sub ListBox2_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox2.MouseDown
        If e.Button = MouseButtons.Right Then
            ListBox2.ContextMenuStrip = ContextMenuStrip1
            ListBox2.ContextMenuStrip.Show()
        End If
    End Sub

    Private Sub ListBox2_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox2.MouseEnter
        ListBox2.Focus()
        Try
            ToolTip1.SetToolTip(ListBox2, arfiles(ListBox2.SelectedIndex))
            ToolTip1.Show(arfiles(ListBox2.SelectedIndex), ListBox2)
        Catch
        End Try
    End Sub

    Private Sub ListBox2_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox2.MouseHover
        ' ToolTip1.SetToolTip(ListBox2, arfiles(ListBox2.SelectedItem - 1))
        ListBox2.Focus()
    End Sub

    Private Sub ListBox2_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox2.MouseLeave
        ToolTip1.Hide(ListBox2)

    End Sub

    Private Sub ListBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox2.SelectedIndexChanged
        Cb_comp_speciale.CheckState = CheckState.Unchecked
        Try

            If arfiles.Item(ListBox2.SelectedIndex).ToString.EndsWith(".pdf") Then
                Label24.Text = ""
                ListBox3.Items.Clear()
                Cb_comp_speciale.CheckState = CheckState.Unchecked
                AxVecad1.Hide()
                AxAcroPDF1.Show()
                'AxAcroPDF1.LoadFile(arfiles.Item(ListBox2.SelectedIndex).ToString)

                comp_pdf.dati_stampa.file_origine = arfiles.Item(ListBox2.SelectedIndex).ToString
                comp_pdf.dati_stampa.RUN_PDF(comp_pdf.dati_stampa.drawstate, arfiles.Item(ListBox2.SelectedIndex).ToString, "")
                ' comp_pdf.UserControl1.filesorgente_TextChanged(Nothing, Nothing)
                AxAcroPDF1.LoadFile(comp_pdf.dati_stampa.file_modificato)

                '------per la ricerca nel database------
                Dim file = arfiles.Item(ListBox2.SelectedIndex).ToString
                Dim estensione As String
                Dim nome As String
                Dim cartella As String
                Dim tabella = DbporteclientiDataSet1.PORTE_SPECIALI
                Dim tabellaMVA = DbporteclientiDataSet1.MVA
                Dim riga As ArrayList

                estensione = file.Remove(0, file.Count - 4)
                cartella = My.Computer.FileSystem.GetParentPath(file)
                Dim nomes = file.Split("\")
                nome = nomes(nomes.Count - 1)
                nome = nome.Remove(nome.Count - 4, 4)

                'cerco la riga nel database e riempio se esiste

                If nome.EndsWith("P") Or nome.EndsWith("p") Then

                    Panel2.Enabled = True
                    ListBox3.Items.Clear()
                    disabilita_campi_db()

                    TBINTERRUTTORE.Enabled = True
                    CBLARGHEZZA.Enabled = True
                    TBALTEZZA.Enabled = True
                    TBINT_X.Enabled = True
                    TBDIS_Y.Enabled = True
                    CBIP.Enabled = True
                    CBORIENTAEMNTO.Enabled = True
                    CBLATOPORTA.Enabled = True
                    TBPORTA.Enabled = True
                    'CBRIALZO_PIASTRA.Enabled = True
                    CBCABLAGGIO.Enabled = True



                    riga = Clsdatabase.findrow(tabella, tabella.Columns("PORTA"), nome)

                    '---prova---
                    If riga.Count > 0 Then
                        For Each item In riga
                            ListBox3.Items.Add(item)
                        Next
                        If ListBox3.Items.Count = 1 Then
                            ListBox3.SelectedItem = ListBox3.Items.Item(0)
                        End If
                    Else
                        Clsdatabase.azzeracampi()
                        TBPORTA.Text = nome.ToUpper
                        TBCARTELLA.Text = cartella
                        'Button4_Click(sender, e)
                    End If

                    '---prova---
                    'If riga <> Nothing Then
                    'Clsdatabase.ricercadb(tabella, riga)
                    'Else
                    'Clsdatabase.azzeracampi()
                    'TBPORTA.Text = nome.ToUpper
                    'TBCARTELLA.Text = cartella
                    'Button4_Click(sender, e)
                    'End If

                ElseIf nome.EndsWith("C") Or nome.EndsWith("c") Then

                    Panel2.Enabled = True
                    ListBox3.Items.Clear()
                    disabilita_campi_db()

                    TBINTERRUTTORE.Enabled = True
                    CBLARGHEZZA.Enabled = True
                    TBALTEZZA.Enabled = True
                    TBINT_X.Enabled = True
                    TBDIS_Y.Enabled = True
                    CBORIENTAEMNTO.Enabled = True
                    CBCABLAGGIO.Enabled = True
                    TBFONDOCELLA.Enabled = True


                    riga = Clsdatabase.findrow(tabella, tabella.Columns("FONDOCELLA"), nome)

                    If riga.Count > 0 Then
                        For Each item In riga
                            ListBox3.Items.Add(item)
                        Next
                        If ListBox3.Items.Count = 1 Then
                            ListBox3.SelectedItem = ListBox3.Items.Item(0)
                        End If
                    Else
                        Clsdatabase.azzeracampi()
                        TBFONDOCELLA.Text = nome
                        TBCARTELLA.Text = cartella
                        'Button4_Click(sender, e)
                    End If



                ElseIf nome.EndsWith("R") Or nome.EndsWith("r") Then
                    Panel2.Enabled = True
                    ListBox3.Items.Clear()
                    disabilita_campi_db()

                    TBINTERRUTTORE.Enabled = True
                    CBLARGHEZZA.Enabled = True
                    TBINT_X.Enabled = True
                    CBIP.Enabled = True
                    tbripiano.Enabled = True

                    riga = Clsdatabase.findrow(tabella, tabella.Columns("RIPIANO"), nome)

                    If riga.Count > 0 Then
                        For Each item In riga
                            ListBox3.Items.Add(item)
                        Next
                        If ListBox3.Items.Count = 1 Then
                            ListBox3.SelectedItem = ListBox3.Items.Item(0)
                        End If
                    Else

                        Clsdatabase.azzeracampi()
                        tbripiano.Text = nome.ToUpper
                        TBCARTELLA.Text = cartella
                        'Button4_Click(sender, e)
                    End If

                ElseIf nome.EndsWith("MS") Or nome.EndsWith("ms") Then
                    GoTo MONTANTE

                ElseIf nome.EndsWith("S") Or nome.EndsWith("s") Then
                    Panel2.Enabled = True
                    ListBox3.Items.Clear()
                    disabilita_campi_db()

                    TBINTERRUTTORE.Enabled = True
                    CBLARGHEZZA.Enabled = True
                    TBALTEZZA.Enabled = True
                    TBINT_X.Enabled = True
                    TBDIS_Y.Enabled = True
                    tbsegregazione.Enabled = True

                    riga = Clsdatabase.findrow(tabella, tabella.Columns("SEGREGAZIONE"), nome)

                    If riga.Count > 0 Then
                        For Each item In riga
                            ListBox3.Items.Add(item)
                        Next
                        If ListBox3.Items.Count = 1 Then
                            ListBox3.SelectedItem = ListBox3.Items.Item(0)
                        End If
                    Else
                        Clsdatabase.azzeracampi()
                        tbsegregazione.Text = nome.ToUpper
                        TBCARTELLA.Text = cartella
                        'Button4_Click(sender, e)
                    End If


                ElseIf nome.EndsWith("MD") Or nome.EndsWith("md") Or nome.EndsWith("MS") Or nome.EndsWith("ms") Or nome.EndsWith("M") Or nome.EndsWith("m") Then
MONTANTE:
                    '------è UN MONTANTE PER APERTO---------
                    'DISABILITO TUTTI I COMPONENTI DEL PANEL2
                    Panel2.Enabled = True
                    ListBox3.Items.Clear()
                    disabilita_campi_db()
                    Clsdatabase.azzeracampi()
                    'ABILITO LA RICERCA DEI MONTANTI

                    Panel3.Enabled = True
                    TBCARTELLA.Enabled = True
                    riga = Clsdatabase.findrow(tabellaMVA, tabellaMVA.Columns("MONTANTE"), nome)
                    If riga.Count > 0 Then
                        For Each item In riga
                            ListBox3.Items.Add(item)
                        Next
                        If ListBox3.Items.Count = 1 Then
                            ListBox3.SelectedItem = ListBox3.Items.Item(0)
                        End If
                    Else
                        Clsdatabase.azzera_campi_MVA()
                        tbmontante.Text = nome.ToUpper
                        TBCARTELLA.Text = cartella
                        'Button4_Click(sender, e)
                    End If

                ElseIf ListBox3.Items.Count = 0 Then
                    Panel2.Enabled = True
                    Dim DATASET As System.Data.DataSet = Clsdatabase.FIND_ROW_GEN("COMP_VARI", "CODICE", "'" & nome & "'")
                    If DATASET.Tables(0).Rows.Count > 0 Then
                        For I = 0 To DATASET.Tables(0).Rows.Count - 1
                            ListBox3.Items.Add(DATASET.Tables(0).Rows(I).Item("ID"))
                        Next
                        Cb_comp_speciale.CheckState = CheckState.Checked
                        If ListBox3.Items.Count = 1 Then
                            ListBox3.SelectedItem = ListBox3.Items.Item(0)
                        End If
                    End If
                Else
                    Panel2.Enabled = True
                End If

                '---------------------------------------

            ElseIf arfiles.Item(ListBox2.SelectedIndex).ToString.EndsWith(".dxf") Then
                AxAcroPDF1.Hide()
                AxVecad1.Show()
                AxVecad1.FileOpen(arfiles.Item(ListBox2.SelectedIndex).ToString)
                AxVecad1.Execute(CAD_CMD_ZOOM_EXT)
                Button2.Enabled = False


            End If
            ToolTip1.SetToolTip(ListBox2, arfiles(ListBox2.SelectedIndex))
            ToolTip1.Show(arfiles(ListBox2.SelectedIndex), ListBox2)

        Catch
        End Try
        ListBox2.Focus()
    End Sub

    Private Sub ListBox3_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox3.SelectedIndexChanged
        Dim riga As Integer
        riga = ListBox3.SelectedItem
        Dim tabellaporte = DbporteclientiDataSet1.PORTE_SPECIALI
        Dim tabellamontanti = DbporteclientiDataSet1.MVA
        Dim NOME = ListBox2.SelectedItem.ToString
        NOME = NOME.Remove(NOME.Count - 4, 4)

        If Cb_comp_speciale.CheckState = CheckState.Checked Then
            Dim DATASET As New System.Data.DataSet
            DATASET = Clsdatabase.FIND_ROW_GEN("COMP_VARI", "ID", riga)
            TBPORTA.Text = DATASET.Tables(0).Rows(0).Item("CODICE")
            CBLARGHEZZA.Text = DATASET.Tables(0).Rows(0).Item("DIM_L")
            TBALTEZZA.Text = DATASET.Tables(0).Rows(0).Item("DIM_H")
            Tb_prof.Text = DATASET.Tables(0).Rows(0).Item("DIM_P")
            Cb_tipologia.Text = DATASET.Tables(0).Rows(0).Item("TIPOLOGIA")
            Tb_note.Text = DATASET.Tables(0).Rows(0).Item("NOTE0")
            TBCARTELLA.Text = DATASET.Tables(0).Rows(0).Item("PERCORSO")
        Else
            If NOME.EndsWith("MD") Or NOME.EndsWith("md") Or NOME.EndsWith("MS") Or NOME.EndsWith("ms") Or NOME.EndsWith("M") Or NOME.EndsWith("m") Then
                Clsdatabase.ricercadb_mva(tabellamontanti, riga)
            Else
                Clsdatabase.ricercadb(tabellaporte, riga)
            End If
        End If
        
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ricerca = TextBox1.Text.ToLower
        ListBox1.Items.Clear()
        If ricerca = Nothing Then
            ricerca = "*"
        End If
        If rcommesse.Count > 0 Then
            For Each cartella In rcommesse
                If cartella.ToString.ToLower.Contains(ricerca) Then
                    ListBox1.Items.Add(cartella)
                End If
            Next
        Else
            For Each cartella In commesse
                If cartella.ToString.ToLower.Contains(ricerca) Then
                    ListBox1.Items.Add(cartella)
                End If
            Next
        End If

    End Sub

    Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
        If e.KeyCode = Keys.Enter Then
            Button1_Click(sender, e)
        End If

        If e.KeyCode = Keys.F12 Then
            Panel2.Enabled = True
        End If

        If e.KeyCode = Keys.F11 Then
            TBPORTA.Enabled = True
            TBFONDOCELLA.Enabled = True
            CBCABLAGGIO.Enabled = True
            CBRIALZO_PIASTRA.Enabled = True
            TBINTERRUTTORE.Enabled = True
            CBLARGHEZZA.Enabled = True
            TBALTEZZA.Enabled = True
            CBIP.Enabled = True
            CBLATOPORTA.Enabled = True
            CBORIENTAEMNTO.Enabled = True
            TBINT_X.Enabled = True
            TBDIS_Y.Enabled = True
            tbripiano.Enabled = True
            tbsegregazione.Enabled = True
            tbmontante.Enabled = True
            cbaltezza_montante.Enabled = True
            tbpart_taglio1.Enabled = True
            tbpart_taglio2.Enabled = True
            tbalt_taglio1.Enabled = True
            tbalt_taglio2.Enabled = True
        End If

        If e.KeyCode = Keys.F10 Then
            Button4_Click(sender, e)
        End If
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        If ListBox2.SelectedItem <> Nothing Then
            If MsgBox("Vuoi cancellare il file selezionato?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
                My.Computer.FileSystem.DeleteFile(arfiles(ListBox2.SelectedIndex))
                ListBox2.Items.RemoveAt(ListBox2.SelectedIndex)
            End If
        End If

        ListBox2.Refresh()
    End Sub


    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Label24.Text = ""
        If Cb_comp_speciale.CheckState = CheckState.Checked Then
            Dim CODICE = TBPORTA.Text
            Dim DIM_L = CBLARGHEZZA.Text
            If DIM_L = "" Then
                DIM_L = "0"
            End If
            Dim DIM_P = Tb_prof.Text
            If DIM_P = "" Then
                DIM_P = "0"
            End If
            Dim DIM_H = TBALTEZZA.Text
            If DIM_H = "" Then
                DIM_H = "0"
            End If
            Dim TIPOLOGIA = Cb_tipologia.Text
            Dim PERCORSO = TBCARTELLA.Text
            Dim NOTE = Tb_note.Text

            Clsdatabase.inserisci_riga_gen("INSERT INTO COMP_VARI(CODICE, DIM_L, DIM_P, DIM_H, TIPOLOGIA, PERCORSO, NOTE0) VALUES('" & CODICE & "', " & DIM_L & ", " & DIM_P & ", " & DIM_H & ", '" & TIPOLOGIA & "', '" & PERCORSO & "', '" & NOTE & "')")

            azzera_campi_db()
            Button4_Click(sender, e)

            For Each ITEM In Clsdatabase.Lista_item("COMP_VARI", "TIPOLOGIA")
                If Cb_tipologia.Items.Contains(ITEM) Then
                Else
                    Cb_tipologia.Items.Add(ITEM)
                End If
            Next

        ElseIf Cb_comp_speciale.CheckState = CheckState.Unchecked Then

            If TBPORTA.Text.EndsWith("P") Or TBPORTA.Text.EndsWith("p") Then
                If TBINTERRUTTORE.Text = "" Then
                    Label24.Text = "Inserisci l'interruttore"
                    GoTo CONTINUA
                End If

                If CBLARGHEZZA.SelectedItem = "" Then
                    Label24.Text = "Inserisci la larghezza"
                    GoTo CONTINUA
                End If

                If TBALTEZZA.Text = "" Then
                    Label24.Text = "Inserisci l'altezza"
                    GoTo CONTINUA
                End If

                If CBIP.SelectedItem = "" Then
                    Label24.Text = "Inserisci l'IP"
                    GoTo CONTINUA
                End If

                If CBLATOPORTA.SelectedItem = "" Then
                    Label24.Text = "Inserisci il lato cerniere"
                    GoTo CONTINUA
                End If

                If CBCABLAGGIO.SelectedItem = "PI" Or CBCABLAGGIO.SelectedItem = "TIC" Or CBCABLAGGIO.SelectedItem = "TIC/G" Then
                    If CBRIALZO_PIASTRA.Text = "" Then
                        Label24.Text = "Inserisci il rialzo"
                        GoTo CONTINUA
                    End If

                ElseIf CBCABLAGGIO.SelectedItem = "ALTRO" Then
                    CBORIENTAEMNTO.Text = ""
                    CBORIENTAEMNTO.Enabled = False
                    GoTo INSERISCI_RIGA_PORTA
                End If

                If CBORIENTAEMNTO.SelectedItem = "" Then
                    If TBINTERRUTTORE.Text.Contains("STR") Or TBINTERRUTTORE.Text.Contains("str") Or TBINTERRUTTORE.Text.Contains("GRV") Then
                    Else
                        Label24.Text = "Inserisci l'orientamento"
                        GoTo CONTINUA
                    End If
                End If

            End If

            If TBFONDOCELLA.Text <> "" Then
                If TBINTERRUTTORE.Text = "" Then
                    Label24.Text = "Inserisci l'interruttore"
                    GoTo CONTINUA
                End If

                If CBLARGHEZZA.SelectedItem = "" Then
                    Label24.Text = "Inserisci la larghezza"
                    GoTo CONTINUA
                End If

                'If CBIP.SelectedItem = "" Then
                '    Label24.Text = "Inserisci l'IP"
                '    GoTo CONTINUA
                'End If
            End If

            If tbripiano.Text <> "" Then

                TBINTERRUTTORE.Enabled = True

                If TBINTERRUTTORE.Text = "" Then
                    Label24.Text = "Inserisci l'interruttore"
                    GoTo CONTINUA
                End If

                If CBLARGHEZZA.SelectedItem = "" Then
                    Label24.Text = "Inserisci la larghezza"
                    GoTo CONTINUA
                End If

                If CBIP.SelectedItem = "" Then
                    Label24.Text = "Inserisci l'IP"
                    GoTo CONTINUA
                End If

            End If

            If tbmontante.Text = "" Then
INSERISCI_RIGA_PORTA:
                If ListBox3.Items.Count < 0 Then
                    Clsdatabase.inserisci_riga(DbporteclientiDataSet1.PORTE_SPECIALI)
                    BindingSource1.EndEdit()
                Else
                    Clsdatabase.inserisci_riga(DbporteclientiDataSet1.PORTE_SPECIALI, ListBox3.SelectedItem)
                    BindingSource1.EndEdit()
                End If

                Try
                    TableAdapterManager.PORTE_SPECIALITableAdapter.Update(DbporteclientiDataSet1)
                    '---chiudo la finestrella di inserimento db---
                    Button4_Click(sender, e)
                Catch ex As Exception
                    'Label24.Text = "Db non aggiornato contattare sviluppatore"
                End Try

                '------CARICO I RIALZI NELLA LISTA DEI RIALZI------
                CBRIALZO_PIASTRA.Items.Clear()
                For Each item In Clsdatabase.CARICA_DATI(DbporteclientiDataSet1.PORTE_SPECIALI, DbporteclientiDataSet1.PORTE_SPECIALI.Columns("RIALZO/PIASTRA"))
                    CBRIALZO_PIASTRA.Items.Add(item)
                Next
                '---------------------------------------------------
            Else
                Clsdatabase.INSERISCI_RIGA_MVA(DbporteclientiDataSet1.MVA)
                BindingSource1.EndEdit()
                Try
                    Me.MVATableAdapter.Update(DbporteclientiDataSet1.MVA)
                    '---chiudo la finestrella di inserimento db---
                    Button4_Click(sender, e)
                Catch ex As Exception
                    Label24.Text = "Db non aggiornato contattare sviluppatore"
                End Try
            End If

CONTINUA:

        End If

        For Each ITEM In Clsdatabase.Lista_item("PORTE_SPECIALI", "INTERRUTTORE")
            If TBINTERRUTTORE.Items.Contains(ITEM) Then
            Else
                TBINTERRUTTORE.Items.Add(ITEM)
            End If
        Next
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim SIZE As New Size

        If Button4.Text = "◄" Then

            SIZE.Width = 640
            SIZE.Height = 495

            Dim PUNTO As New Point
            PUNTO.X = Me.Size.Width - SIZE.Width
            PUNTO.Y = Me.Size.Height - SIZE.Height - 39

            Panel2.Parent = Me
            Panel2.SetBounds(PUNTO.X, PUNTO.Y, SIZE.Width, SIZE.Height)
            Button4.Text = "►"
        Else
            SIZE.Width = 143
            SIZE.Height = 382

            Dim PUNTO As New Point
            PUNTO.X = Me.Size.Width - SIZE.Width - 25
            PUNTO.Y = Me.Size.Height - SIZE.Height - 39

            Panel2.Parent = Me
            Panel2.SetBounds(PUNTO.X, PUNTO.Y, SIZE.Width, SIZE.Height)
            Button4.Text = "◄"
        End If
    End Sub

    Private Sub RinominaToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RinominaToolStripMenuItem.Click
        frmrinomina.carica_dati(arfiles.Item(ListBox2.SelectedIndex))
        frmrinomina.ShowDialog(Me)
    End Sub

    Private Sub disabilita_campi_db()
        TBPORTA.Enabled = False
        TBALTEZZA.Enabled = False
        TBCARTELLA.Enabled = False
        TBDIS_Y.Enabled = False
        TBFONDOCELLA.Enabled = False
        TBINT_X.Enabled = False
        TBINTERRUTTORE.Enabled = False
        CBCABLAGGIO.Enabled = False
        CBIP.Enabled = False
        CBLARGHEZZA.Enabled = False
        CBORIENTAEMNTO.Enabled = False
        CBRIALZO_PIASTRA.Enabled = False
        CBLATOPORTA.Enabled = False
        tbripiano.Enabled = False
        tbsegregazione.Enabled = False
        Panel3.Enabled = False
        Tb_prof.Enabled = False
        Cb_tipologia.Enabled = False
        Tb_note.Enabled = False
    End Sub

    Private Sub CBCABLAGGIO_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CBCABLAGGIO.SelectedIndexChanged
        If CBCABLAGGIO.SelectedItem = "PI" Or CBCABLAGGIO.SelectedItem = "TIC" Or CBCABLAGGIO.SelectedItem = "TIC/G" Then
            CBRIALZO_PIASTRA.Enabled = True
        Else
            CBRIALZO_PIASTRA.Enabled = False
        End If
    End Sub


    Private Sub ListBox3_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles ListBox3.KeyDown
        If e.KeyCode = Keys.Delete Then
            Clsdatabase.elimina_riga(DbporteclientiDataSet1.PORTE_SPECIALI, ListBox3.SelectedItem)
            BindingSource1.EndEdit()
            Try
                TableAdapterManager.PORTE_SPECIALITableAdapter.Update(DbporteclientiDataSet1.PORTE_SPECIALI)
            Catch ex As Exception
            End Try
            ListBox3.Items.Remove(ListBox3.SelectedItem)
        End If
    End Sub

    Private Sub ModificaPDFToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ModificaPDFToolStripMenuItem.Click
        If arfiles.Item(ListBox2.SelectedIndex).ToString.EndsWith(".pdf") Then
            Dim processo = Process.Start(My.Computer.FileSystem.SpecialDirectories.ProgramFiles & "\pdf editor 2\pdfedit.exe", arfiles.Item(ListBox2.SelectedIndex).ToString)
            While Not processo.HasExited
                Continue While
            End While
        End If
    End Sub

    Private Sub Cb_comp_speciale_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cb_comp_speciale.CheckedChanged
        If Cb_comp_speciale.CheckState = CheckState.Checked Then

            azzera_campi_db()
            disabilita_campi_db()

            TBPORTA.Enabled = True
            CBLARGHEZZA.Enabled = True
            TBALTEZZA.Enabled = True
            Tb_prof.Enabled = True
            Cb_tipologia.Enabled = True
            Tb_note.Enabled = True

            Dim file = arfiles.Item(ListBox2.SelectedIndex).ToString
            Dim estensione As String
            Dim nome As String
            Dim cartella As String

            estensione = file.Remove(0, file.Count - 4)
            cartella = My.Computer.FileSystem.GetParentPath(file)
            Dim nomes = file.Split("\")
            nome = nomes(nomes.Count - 1)
            nome = nome.Remove(nome.Count - 4, 4)

            TBPORTA.Text = nome
            TBCARTELLA.Text = cartella

        End If
    End Sub

    Private Sub azzera_campi_db()
        TBPORTA.Text = ""
        TBALTEZZA.Text = ""
        TBCARTELLA.Text = ""
        TBDIS_Y.Text = ""
        TBFONDOCELLA.Text = ""
        TBINT_X.Text = ""
        TBINTERRUTTORE.Text = ""
        CBCABLAGGIO.Enabled = False
        CBIP.Enabled = False
        CBLARGHEZZA.Enabled = False
        CBORIENTAEMNTO.Enabled = False
        CBRIALZO_PIASTRA.Enabled = False
        CBLATOPORTA.Enabled = False
        tbripiano.Text = ""
        tbsegregazione.Text = ""
        Tb_prof.Text = ""
        Cb_tipologia.Text = ""
        Cb_tipologia.Enabled = False
        Tb_note.Text = ""

    End Sub

    Private Sub FrmDisegniClienti_Activated(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Activated
        Timer1.Enabled = True
        comp_pdf.dati_stampa.Tempfile = "Temp6"
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        If temp1 Is Nothing Then
            If comp_pdf.dati_stampa.file_modificato Is Nothing Then
            Else
                temp1 = My.Computer.FileSystem.GetFileInfo(Application.StartupPath & "\Temp6.pdf")
            End If

        Else
            Dim time As System.IO.FileInfo
            time = My.Computer.FileSystem.GetFileInfo(Application.StartupPath & "\Temp6.pdf")

            If time.LastWriteTime > temp1.LastWriteTime Then
                AxAcroPDF1.LoadFile(Application.StartupPath & "\Temp6.pdf")
                temp1 = time
            End If
        End If
    End Sub
End Class